/*
*********************************************************************************
Description:	Produces results by audit outcome, comparing audited individuals
				to individuals who _will be_ audited

Uses:			"$mergedata\Couldhavesample.dta"	

Saves:			-	

*********************************************************************************
*/


*******************
* SET UP THE DATA *
*******************

use "$mergedata\CouldhaveSample.dta", clear

keep if treatment //keep only the people selected for a random audit
assert random_sig!="N" if yrssince==0

*tidy up close_co info
**********************
* don't want to lose obs for having missing info on close company status 
recode close_co (.=2)
label define close_co 0 "no close company" 1 "closely held company" 2 "missing close company info"
label val close_co close_co 


*CONSTRUCT AUDIT OUTCOME VARIABLES
**********************************

*audit result
gen byte correctTEMP = (stlmnt_class_ind == "A") & audityear==tax_year
gen byte mistake_nonposTEMP = (stlmnt_class_ind == "B") & audityear==tax_year
gen byte mistake_posTEMP = (stlmnt_class_ind == "C") & audityear==tax_year
gen byte guiltyTEMP = inlist(stlmnt_class_ind,"1","2","3") & audityear==tax_year

egen byte correct=max(correctTEMP),by(utr_no audityear)
egen byte mistake_nonpos=max(mistake_nonposTEMP),by(utr_no audityear)
egen byte mistake_pos=max(mistake_posTEMP),by(utr_no audityear)
egen byte guilty=max(guiltyTEMP),by(utr_no audityear)
egen byte audited=max(actuallyaudited),by(utr_no audityear)
gen  byte notaudited=1-audited

*generate a variable containing audit status
gen byte aud_stat = .
replace aud_stat = 1 if correct
replace aud_stat = 2 if mistake_nonpos
replace aud_stat = 3 if mistake_pos
replace aud_stat = 4 if guilty
replace aud_stat = 5 if notaudited
assert aud_stat!=.
assert correct + mistake_pos + mistake_nonpos + guilty + notaudited ==1
label define aud_stat 1 "correct" 2 "mistake, nonpos yield" 3 "mistake, pos yield" 4 "guilty (pos yield+penalty)" 5 "not audited"
label value aud_stat aud_stat



*******************************
* CREATE THE OUTCOME VARIABLE *
*******************************

*Step 1:  generate a variable = tax_{-1}, in case we want this
tempvar tax_neg1_temp tax_neg1 tax_t_neg1
gen double `tax_neg1_temp' = it_cl4_cgt_cpi if yrssince==-1
gegen double `tax_neg1'    = max(`tax_neg1_temp'), by(id)

*Step 2:  generate an intermediate variable = tax_{t} - tax_{-1}
gen `tax_t_neg1' = it_cl4_cgt_cpi - `tax_neg1'

*Step 3:  gen final outcome var = 1[tax_{t} - tax_{-1}>=0]
gen increase_t_neg1 = `tax_t_neg1'>=0




************************************
* TABLE 5: IMPACT BY AUDIT OUTCOME *
************************************

*baseline again
local audval=0
eststo increase_aud`audval', t("overall"): qui reg increase_t_neg1 i.yrssince20 i.female c.age#c.age c.yrsfiling c.maxyrssince i.industry i.const_in i.close_co i.gorcode ///
		if survives& yrsfiling>4 & tax_year >2001 & tax_year <2009, cl(id)	

*by audit outcome
forvalues audval=1/5 {
	if `audval'==1 local audtext "correct"  
	if `audval'==2 local audtext "mistake, nonpos yield" 
	if `audval'==3 local audtext "mistake, pos yield" 
	if `audval'==4 local audtext "guilty (pos yield+penalty)" 
	if `audval'==5 local audtext "not audited" 
	eststo increase_aud`audval', t("`audtext'"): qui reg increase_t_neg1 i.yrssince20 i.female c.age#c.age c.yrsfiling c.maxyrssince i.industry i.const_in i.close_co i.gorcode ///
		if survives& yrsfiling>4 & tax_year >2001 & tax_year <2009 & aud_stat==`audval', cl(id)	
}
esttab increase_aud0 increase_aud1 increase_aud2 increase_aud3 increase_aud4 increase_aud5,  ///
	keep(14.yrssince20 15.yrssince20 16.yrssince20 17.yrssince20 18.yrssince20 19.yrssince20 ///
	20.yrssince20 21.yrssince20 22.yrssince20 23.yrssince20 24.yrssince20 25.yrssince20)  	 ///
	nodep mtitle se
	
esttab increase_aud0 increase_aud1 increase_aud2 increase_aud3 increase_aud4 increase_aud5,  ///
	nodep mtitle se

